import api from '@/http/api'

const SET_DETAIL = 'SET_DETAIL'
const SET_COLLECT_INFO_VISIBLE = 'SET_COLLECT_INFO_VISIBLE'
const SET_IM = 'SET_IM'

export default {
    namespaced: true,
    state: {
        detail: {},
        im: {},
        collectInfoVisible: false
    },
    mutations: {
        [SET_DETAIL] (state, val = {}) {
            state.detail = val
            sessionStorage.setItem('liveId', val.id)
            sessionStorage.setItem('projectId', val.projectId)
        },
        [SET_COLLECT_INFO_VISIBLE] (state, val = false) {
            state.collectInfoVisible = val
        },
        [SET_IM] (state, val = {}) {
            state.im = val
        }
    },
    actions: {
        async apiGetLiveDetail ({ commit, dispatch }, val) {
            const res = await api.getLiveDetail({
                id: val.id,
                auid: val.auid
            })
            if (!res.code) {
                commit('SET_DETAIL', res.data)
                document.title = res.data.topic
                // if (!res.data.hasCollectAudienceMsg || !res.data.projectVo?.collectAudienceMsg) {
                //     dispatch('apiGetImInfo', val)
                // }
            }
        },
        async apiGetImInfo ({ commit }, val) {
            const res = await api.getImId({
                id: val.id,
                auid: val.auid
            })
            if (!res.code) {
                commit('SET_IM', res.data)
            }
        }
    }
}
